#include "../include/stack.h"

#include <stdlib.h>

void InitStack( ListStack *s )
{
    ( *s ) = NULL;
}

BOOL StackEmpty( ListStack *s )
{
    if ( (*s) == NULL ) {
        return TRUE;
    } else {
        return FALSE;
    }
}

Status GetTop( ListStack *s, StackNodeType *pTop )
{
    if ( StackEmpty( s ) || pTop == NULL ) {
        return ERROR;
    }

    *pTop = (*s)->data;

    return OK;
}

Status Pop( ListStack *s, StackNodeType *pTop )
{
    StackNode *p = NULL;

    if ( StackEmpty( s ) || pTop == NULL ) {
        return ERROR;
    }

    *pTop = (*s)->data;
    p = *s;
    *s = (*s)->next;

    free( p );

    return OK;
}

Status Push( ListStack *s, StackNodeType *pdata )
{
    StackNode *p = ( StackNode * )malloc( sizeof( StackNode ) );

    if ( p == NULL ) {
        return ERROR;
    }

    p->data = *pdata;
    p->next = *s;
    *s = p;

    return OK;
}


